package acwing;

import java.io.*;
import java.util.Arrays;

public class A853 {
    static BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter log=new BufferedWriter(new OutputStreamWriter(System.out));
    static int n;
    static int m;
    static int k;
    static class Edge{
        int from;
        int to;
        int val;
        Edge(int from,int to,int val){
            this.from=from;
            this.to=to;
            this.val=val;
        }
    }
static int N=10010;
   static Edge[] edges=new Edge[N];
    static int[] dist=new int[N];//最小距离
    static int[] copy;
    public static void main(String[] args) throws IOException {
        String[] s = read.readLine().split(" ");
        n=Integer.parseInt(s[0]);
        m=Integer.parseInt(s[1]);

        for(int i=1;i<=m;i++){
            String[] s1 = read.readLine().split(" ");
            int i1 = Integer.parseInt(s1[0]);
            int i2 = Integer.parseInt(s1[1]);
            int i3 = Integer.parseInt(s1[2]);
edges[i]=new Edge(i1,i2,i3);
        }
        int t= bellmanFord();
        if(t==0x3f3f3f3f) System.out.println("impossible");
        else System.out.println(dist[n]);
    }
    static int bellmanFord(){
        Arrays.fill(dist,0x3f3f3f3f);
        dist[1]=0;
        //k次松弛操作
        for(int i=1;i<=n-1;i++){
          copy=Arrays.copyOf(dist,N);
            for(int j=1;j<=m;j++){
                Edge edge = edges[j];
                int from= edge.from;
                int to= edge.to;
                int val=edge.val;
                dist[to]=Math.min(dist[to],copy[from]+val);
            }
        }
            return   dist[n]>0x3f3f3f3f/2?0x3f3f3f3f:dist[n];
    }
}
